<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  admin_activity.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-19.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Admin_activity extends CI_Controller {

	 function __construct()
	 {
		parent::__construct();
		$this->fromsource = $this->config->item('fromsource');
		//加载缓存文件
		$this->brands = $this->config->item('brand');
		
 		$this->load->model('common/security_model');
 		$this->load->model('admin/admin_activity_model');
 		$this->load->model('activity_model');
		if($this->security_model->isAdminLogin() === FALSE){
          showmessage('请登录', 'admin/login/index', 3);
		}
		//加载缓存类
		$this->load->library('datacache');
		
	 }

	function index()
	{
		$data = $this->admin_activity_model->get_activitys();
		// 分类缓存
		$data['category'] = $this->config->item('category');
		//来源缓存
		$data['fromsource'] = $this->fromsource;
		$this->load->template('admin/activity_tpl', $data);
	}
	
	function add_activity(){
		$set = $this->input->post('set');
		$thevaluesubmit = $this->input->post('thevaluesubmit') ? $this->input->post('thevaluesubmit') : '';
		if ($thevaluesubmit) {
			
			//验证表单
			$insertdata = array(
				'title' => $set['title'],
				'imageurl' => $set['imageurl'],
				'img_mode' => $set['img_mode'],
				'url' => $set['url'],
				'brandid' => $set['brandid'],
				'catid' => $set['catid'],
				'fromid' => $set['fromid'],
				'discount' => $set['discount'],
				'displayorder' => $set['displayorder'] ? intval($set['displayorder']) : 0,
				'starttime' => strtotime($set['stime']),
				'endtime' => strtotime($set['etime']),
				'status' => '-2',
				'dateline' => now(),
				);
			$aid = $this->activity_model->insert_activity($insertdata);
			if ($set['img_mode'] == 1) {
				// 上传文件
				if ($_FILES['userfile']['name']) {
					$config = array();
					$config['file_name'] = 'act_'.$aid.'_'.date('Ymd',now());
					$result = upload_file($config);
					if ($result['error']) {
						showmessage('上传失败','admin/admin_activity/index', 3);				
					}else{
						$updatedata = array(
								'aid' => $aid,
								'uploadimg' => $result['upload_data']['basename'].'/'.$result['upload_data']['file_name'],
							);
						$result = $this->activity_model->update_activity($updatedata);
					}
				}
			}

			if ($aid) {
				//更新主页缓存
				$this->datacache->index_cache();
				showmessage('添加成功','admin/admin_activity/downindex_activity/', 3);
			}else{
				showmessage('添加失败','admin/admin_activity/index', 3);				
			}
			
		}
		//来源缓存
		$data['fromsource'] = $this->fromsource;
		// 分类缓存
		$data['category'] = $this->config->item('category');
		//品牌
		$data['brands'] = $this->brands;
		$this->load->template('admin/add_activity_tpl', $data);		
	}
	
	function edit_activity($aid){
		$set = $this->input->post('set', true);
		$thevaluesubmit = $this->input->post('thevaluesubmit') ? $this->input->post('thevaluesubmit') : '';
		$aid = $aid ? intval($aid) : 0;
		if (empty($aid)) {
			showmessage('非法来源');
		}
		$activity = $this->activity_model->get_activity_by_aid($aid);
		$activity['starttime'] = $activity['starttime'] ? date('Y-m-d', $activity['starttime']) : '';
		$activity['endtime'] = $activity['endtime'] ? date('Y-m-d', $activity['endtime']) : '';
		$activity['uploadimg'] = base_url().'upload/attachments/'.$activity['uploadimg'];
		//表单提交
		if (!empty($thevaluesubmit) && !empty($aid)) {
			//验证表单
			$updatedata = array(
				'aid' => $aid,
				'title' => $set['title'],
				'imageurl' => $set['imageurl'],
				'url' => $set['url'],
				'brandid' => $set['brandid'],
				'fromid' => $set['fromid'],
				'discount' => $set['discount'],
				'displayorder' => $set['displayorder'] ? intval($set['displayorder']) : 0,
				'starttime' => strtotime($set['stime']),
				'img_mode' => $set['img_mode'],
				'catid' => $set['catid'],
				'endtime' => strtotime($set['etime']),
				);
			$id = $this->activity_model->update_activity($updatedata);
			//同步子商品品牌
			if ($set['syncgoods_brand'] && $set['brandid']) {
				$this->db->where('aid', $aid);
				$this->db->update('goods', array('brandid'=>$set['brandid']));
			}
			
			//同步子商品分类
			if ($set['syncgoods_category'] && $set['catid']) {
				$this->db->where('aid', $aid);
				$this->db->update('goods', array('catid'=>$set['catid']));
			}
			
			if ($set['img_mode'] == 1) {
				// 上传文件
				if ($_FILES['userfile']['name']) {
					$config = array();
					$config['file_name'] = 'act_'.$aid.'_'.date('Ymd',now());
					$result = upload_file($config);
					if ($result['error']) {
						showmessage('上传失败','admin/admin_activity/index', 3);				
					}else{
						$updatedata = array(
								'aid' => $aid,
								'uploadimg' => $result['upload_data']['basename'].'/'.$result['upload_data']['file_name'],
							);
						$result = $this->activity_model->update_activity($updatedata);
					}
				}
			}

				//更新主页缓存
				$this->datacache->index_cache();
				showmessage('编辑成功','admin/admin_activity/index', 3);
			
		}
		
		$data['thevalue'] = $activity;
		//来源缓存
		$data['fromsource'] = $this->fromsource;
		// 分类缓存
		$data['category'] = $this->config->item('category');		
		//品牌
		$data['brands'] = $this->brands;
		$this->load->template('admin/edit_activity_tpl', $data);	
	}
	//单个删除 controller
    function delete($aid)
    {
		if (empty($aid)) {
			showmessage('没有选择任何活动');
		}
		
		$ids = array($aid);
		if (empty($ids)) {
			showmessage('没有选择任何活动');
		}
		$result = $this->activity_model->delete_activitys($ids);
		if (is_array($result)) {
			showmessage('id:'.implode(',', $result).' 该活动下还有商品，请删除商品之后再次尝试');
		}else{
			//更新主页缓存
			$this->datacache->index_cache();
			
			showmessage('删除成功','admin/admin_activity/index', 3);	
		}
    }

	//批量删除 controller
	function deletes()
	{	
		$ids = $this->input->post('ids');
		if (empty($ids)) {
			showmessage('没有选择任何活动');
		}
		$result = $this->activity_model->delete_activitys($ids);
		if (is_array($result)) {
			showmessage('id:'.implode(',', $result).'该活动下还有商品，请删除商品之后再次尝试');
		}else{
			//更新主页缓存
			$this->datacache->index_cache();
			
			showmessage('删除成功','admin/admin_activity/index', 3);	
		}
	}
	
	//单个下架
    function downindex($aid)
    {
		if (empty($aid)) {
			showmessage('没有选择任何活动');
		}
		
		$ids = array($aid);
		if (empty($ids)) {
			showmessage('没有选择任何活动');
		}
		$result = $this->activity_model->downindexs($ids);
		if ($result) {
			//更新主页缓存
			$this->datacache->index_cache();
			showmessage('操作成功','admin/admin_activity/downindex_activity/index', 3);	
		}else{
			showmessage('操作失败','admin/admin_activity/index', 3);	
		}
    }

	//批量下架
	function downindexs()
	{	
		$ids = $this->input->post('ids');
		if (empty($ids)) {
			showmessage('没有选择任何活动');
		}
		$result = $this->activity_model->downindexs($ids);
		if ($result) {
			//更新主页缓存
			$this->datacache->index_cache();
			showmessage('操作成功','admin/admin_activity/downindex_activity/index', 3);	
		}else{
			showmessage('操作失败','admin/admin_activity/index', 3);	
		}
	}

	//单个下架
    function upindex($aid)
    {
		if (empty($aid)) {
			showmessage('没有选择任何活动');
		}
		
		$ids = array($aid);
		if (empty($ids)) {
			showmessage('没有选择任何活动');
		}
		$result = $this->activity_model->upindexs($ids);
		if ($result) {
			//更新主页缓存
			$this->datacache->index_cache();
			showmessage('操作成功','admin/admin_activity/index', 3);	
		}else{
			showmessage('操作失败','admin/admin_activity/index', 3);	
		}
    }

	//批量下架
	function upindexs()
	{	
		$ids = $this->input->post('ids');
		if (empty($ids)) {
			showmessage('没有选择任何活动');
		}
		$result = $this->activity_model->upindexs($ids);
		if ($result) {
			//更新主页缓存
			$this->datacache->index_cache();
			showmessage('操作成功','admin/admin_activity/index', 3);	
		}else{
			showmessage('操作失败','admin/admin_activity/index', 3);	
		}
	}
	
	//下架商品
	function downindex_activity(){
		$data = $this->admin_activity_model->get_downindex_activitys();
		$this->load->template('admin/downindex_activity_tpl', $data);
	}
}
?>